<?php
class processModel extends model
{
    public function gettype($pid){
        $types   = $this->dao->select('id, mistake')->from(TABLE_CATEGORY)->where('pid')->eq($pid)->fetchPairs();
        foreach($types as $id => $mistake) if($mistake == '') $types[$id] = $mistake;
        return array('' => '') + $types;
    }
    public function gettypes(){
        $types   = $this->dao->select('id, mistake')->from(TABLE_CATEGORY)->fetchPairs();
        foreach($types as $id => $mistake) if($mistake == '') $types[$id] = $mistake;
        return array('' => '') + $types;
    }
    public function getbigtype(){
        $types   = $this->dao->select('id, mistake')->from(TABLE_BIGCATEGORY)->fetchPairs();
        foreach($types as $id => $mistake) if($mistake == '') $types[$id] = $mistake;
        return array('' => '') + $types;
    }

    public function delprocess($id){

         $this->dao->update(TABLE_PROCESS)->set('revocation')->eq('yes')->where('id')->eq($id)->exec();
        $order = $this->getorders($id);
        $this->dao->update(TABLE_DEDUCTMARKS)->set('revocation')->eq('yes')->where('processid')->eq($order)->exec();
        $this->dao->update(TABLE_LIANDAI)->set('revocation')->eq('yes')->where('orders')->eq($order)->exec();
        $this->dao->update(TABLE_STATISTICS)->set('revocation')->eq('yes')->where('orders')->eq($order)->exec();

        if(!dao::isError()){
            $data = array(
                'erro'=>'1',
                'data'=>'撤销成功',
            );
        }else{
            $data = array(
                'erro'=>'0',
                'data'=>'撤销失败',
            );
        }
        return json_encode($data);
    }

    public function ismonitors($model){
        $user = $_SESSION['user'];
        $unit = $this->dao->select('personnel')->from(TABLE_SETUNIT)->where('model')->eq($model)->fetchPairs();
        $unit = implode(',',$unit);
        $unit = explode(',',$unit);
        if(in_array($user->id_card,$unit)){
            return true;
        }else{
            return false;
        }
    }
    public function getdepartment(){
        $departments = $this->dao->select('unit_id,unit_name')->from(TABLE_UNIT)->fetchPairs();
        foreach($departments as $id=>$name) if($name == '') $departments[$id] = $name;
        return array('' => '') + $departments;
    }
    public function getfile($objectType){
        $file = $this->dao->select('pathname,title')->from(TABLE_FILE)->where('objectID')->eq($objectType)->fetchAll();
        return $file;
    }
    public function getusersbydept($dept){
        $users = $this->dao->select('id_card,name')->from(TABLE_USERS)->where('unit_id')->eq($dept)->fetchPairs();
        foreach($users as $id=>$name) if($name == '') $users[$id] = $name;
        return array('' => '') + $users;
    }
    public function getuser(){
        $users = $this->dao->select('id_card,name')->from(TABLE_USERS)->fetchPairs();
        foreach($users as $id=>$name) if($name == '') $users[$id] = $name;
        return array('' => '') + $users;
    }
    public function getuserbyidcard($idcard){
        $users = $this->dao->select('id_card,name')->from(TABLE_USERS)->where('id_card')->in($idcard)->fetchPairs();
        foreach($users as $id=>$name) if($name == '') $users[$id] = $name;
        return array('' => '') + $users;
    }
    public function getprocess($pager){
        $process = $this->dao->select('*')->from(TABLE_PROCESS)->where('revocation')->ne('yes')->orderBy('id desc')->page($pager)->fetchAll();
        foreach($process as $value){
            $personnel = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->in($value->personnel)->fetchPairs();
            $value->idcard =explode(',',$value->personnel);
            $value->personnel = implode($personnel,',');
            $monitors = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->monitors_idcard)->fetch();
            $value->monitors_idcard = $monitors->name;
            $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->department_heads)->fetch();
            $value->department_heads = $head->name;
        }
        return $process;
    }

    public function getseach($type){
        $user = $_SESSION['user'];
        $field1 = $this->post->field1;
        $operator1 = $this->post->operator1;
        $value1 = $this->post->value1?$this->post->value1:'';
        $groupAndOr = $this->post->groupAndOr;
        $field4 = $this->post->field4;
        $operator4 = $this->post->operator4;
        $value4 = $this->post->value4?$this->post->value4:'';

        $where = "$field1 $operator1 '$value1' $groupAndOr $field4 $operator4 '$value4'";
        if(empty($value4)){
            $where = "$field1 $operator1 '$value1'";
        }


        $process = $this->dao->select('*')->from(TABLE_PROCESS)
            ->where('istrue')->ne('no')
            ->beginIF($value1 !== "" || $value4 !== "")
            ->andWhere($where)->fi()
            ->beginIF($type=='confirm')
            ->andWhere('department_id')->eq($user->unit_id)
            ->andWhere('revocation')->ne('yes')
            ->fi()
            ->beginIF($type=='concerned')
            ->andWhere('personnel')->like('%%'.$user->id_card.'%%')
            ->andWhere('heads_confirm')->eq('yes')
            ->andWhere('revocation')->ne('yes')
            ->fi()
            ->orderBy('id desc')->fetchAll();


        foreach($process as $value){
            $type =  $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($value->category)->fetchPairs();
            $value->category = implode($type,',');
            $bigcategory = $this->dao->select('mistake')->from(TABLE_BIGCATEGORY)->where('id')->eq($value->bigcategory)->fetch();
            $value->bigcategory = $bigcategory->mistake;
            $personnel = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->in($value->personnel)->fetchPairs();
            $value->idcard =explode(',',$value->personnel);
            $value->personnel = implode($personnel,',');
            $monitors = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->monitors_idcard)->fetch();
            $value->monitors_idcard = $monitors->name;
            $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->department_heads)->fetch();
            $value->department_heads = $head->name;
        }
        return $process;

    }

    public function getbydept($pager){
        $user = $_SESSION['user'];
        $process = $this->dao->select('*')->from(TABLE_PROCESS)->where('department_id')->eq($user->unit_id)->andWhere('revocation')->ne('yes')->orderBy('id desc')->page($pager)->fetchAll();
        foreach($process as $value){
            $bigcategory = $this->dao->select('mistake')->from(TABLE_BIGCATEGORY)->where('id')->eq($value->bigcategory)->fetch();
            $value->bigcategory = $bigcategory->mistake;
            $type =  $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($value->category)->fetchPairs();
            $value->category = implode($type,',');
            $personnel = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->in($value->personnel)->fetchPairs();
            $value->idcard =explode(',',$value->personnel);
            $value->personnel = implode($personnel,',');
            $monitors = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->monitors_idcard)->fetch();
            $value->monitors_idcard = $monitors->name;
            $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->department_heads)->fetch();
            $value->department_heads = $head->name;
        }
        return $process;
    }
    public function getprocessbyid($id){
        $process = $this->dao->select('*')->from(TABLE_PROCESS)->where('id')->eq($id)->fetch();
            $type =  $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($process->category)->fetchPairs();
            $process->type = $process->category;
            $process->category = implode($type,',');
            $bigtype = $this->dao->select('mistake')->from(TABLE_BIGCATEGORY)->where('id')->eq($process->bigcategory)->fetch();
            $process->bigtype = $bigtype->mistake;
            $monitors = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($process->monitors_idcard)->fetch();
             $process->monitors_idcard = $monitors->name;
        return $process;
    }
    public function create(){
        $this->app->user = $_SESSION['user'];
        $category1 = array_unique((array)$this->post->type);
        $category2 = array_unique((array)$this->post->type2);
        $category3 = array_unique((array)$this->post->type3);
        $category = array_merge($category1,$category2,$category3);
        $category = array_unique($category);
        $minicategory = $category;
        $category = implode($category,',');

        $bigcategory = $this->post->bigtype;
        $bigcategory2 = $this->post->bigtype2;
        $bigcategory3 = $this->post->bigtype3;
        $bigcategory = array_merge($bigcategory,$bigcategory2,$bigcategory3);
        $bigcategory =array_unique($bigcategory);
        $bigcategory = implode($bigcategory,',');

        $bigcategoryname   = $this->dao->select('id, mistake')->from(TABLE_BIGCATEGORY)->where('id')->in($bigcategory)->fetchPairs();
        $bigcategoryname = implode($bigcategoryname,',');

        $typesname   = $this->dao->select('id, mistake')->from(TABLE_CATEGORY)->where('id')->in($category)->fetchPairs();
        $typesname = implode($typesname,',');


        $person = implode($this->post->concerned,',');
        $m_attachment = time().'matta';
        $deptname = $this->dao->select('unit_name')->from(TABLE_UNIT)->where('unit_id')->eq($this->post->department)->fetch();
        $order = time().'jd';

        $this->dao->insert(TABLE_PROCESS)
            ->set('monitors_idcard')->eq($this->app->user->id_card)
            ->set('monitors_department_id')->eq($this->app->user->unit_id)
            ->set('monitors_department_name')->eq($this->app->user->unit_name)
            ->set('personnel')->eq($person)
            ->set('department_id')->eq($this->post->department)
            ->set('bigcategory')->eq($bigcategory)
            ->set('bigcategoryname')->eq($bigcategoryname)
            ->set('category')->eq($category)
            ->set('categoryname')->eq($typesname)
            ->set('m_attachment')->eq($m_attachment)
            ->set('eventtime')->eq($this->post->eventtime)
            ->set('address')->eq($this->post->address)
            ->set('m_content')->eq($this->post->desc)
            ->set('m_inputtime')->eq(helper::now(time()))
            ->set('department_name')->eq($deptname->unit_name)
            ->set('heads_confirm')->eq('no')
            ->set('approval_status')->eq('wait')
            ->set('orders')->eq($order)
            ->exec();

        $ID = $this->dao->lastInsertID();
        $this->loadModel('file', 'module')->saveUpload('process', $m_attachment);

        return $ID;
    }

    public function setprocess($id){
        $user = $_SESSION['user'];
        $person = implode($this->post->concerned,',');
        $this->dao->update(TABLE_PROCESS)
            ->set('personnel')->eq($person)
            ->set('heads_confirm')->eq('yes')
            ->set('confirmedtime')->eq(helper::now(time()))
            ->set('department_heads')->eq($user->id_card)
            ->set('headcontent')->eq($this->post->headcontent)
            ->where('id')->eq($id)
            ->exec();
        return !dao::isError();
    }
    public function appeal($id){
        $p_attachment = isset($_FILES)?time().'attachment':'';
        $this->dao->update(TABLE_PROCESS)
            ->set('p_category')->eq('confirm')
            ->set('p_content')->eq($this->post->p_content)
            ->set('persontime')->eq(helper::now(time()))
            ->set('p_attachment')->eq($p_attachment)
            ->set('approval_status')->eq('doing')
            ->where('id')->eq($id)
            ->exec();
    if($p_attachment){
        $this->loadModel('file', 'module')->saveUpload('process', $p_attachment);
    }
        return !dao::isError();

    }

    public function getunitname($unit_id){
        $unitname = $this->dao->select('unit_name')->from(TABLE_UNIT)->where('unit_id')->eq($unit_id)->fetch();
        return $unitname->unit_name;
    }
    public function getsort($unit_id){

        $unitname = $this->dao->select('sort')->from(TABLE_UNIT)->where('unit_id')->eq($unit_id)->fetch();
        return $unitname->sort;
    }
    public function getbigcategoryname($bigid){
        $bname = $this->dao->select('mistake')->from(TABLE_BIGCATEGORY)->where('id')->eq($bigid)->fetch();
        return $bname->mistake;
    }

    public function getcategoryname($category){
        $categoryname = $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($category)->fetchPairs();
        $cname = implode($categoryname,',');
        return $cname;
    }

    public function getscore($scoretype,$category){
        $sum = $this->dao->select("id,$scoretype")->from(TABLE_CATEGORY)->where('id')->in($category)->fetchPairs();
        $score = array_sum($sum);
        return $score;
    }
    public function getorders($id){
        $orders = $this->dao->select('orders')->from(TABLE_PROCESS)->where('id')->eq($id)->fetch();
        return $orders->orders;
    }

    public function getparentid($uintid){

        $parentid = $this->dao->select('parent_id')->from(TABLE_UNIT)->where('unit_id')->eq($uintid)->fetch();
        return $parentid->parent_id;
    }

    public function approval($id){
        $istrue = $this->post->istrue?$this->post->istrue:'yes';
        $category = array_unique((array)$this->post->type);
        $scoretype = $this->post->scoretype;
        $scoretypename = $this->lang->process->typeList[$scoretype];
        $minicategory = $category;
        $category = implode($category,',');

        $sum = $this->dao->select("id,$scoretype")->from(TABLE_CATEGORY)->where('id')->in($category)->fetchPairs();
        //return $sum;
       $typesscore = implode($sum,',');

        $typesname   = $this->dao->select('id, mistake')->from(TABLE_CATEGORY)->where('id')->in($category)->fetchPairs();
        $typesname = implode($typesname,',');


        $score = array_sum($sum);
        //return $score;

        if($istrue=='no'){
            $score = 0;
        }
        $headuser = $_SESSION['user'];
        $process = $this->dao->select('*')->from(TABLE_PROCESS)->where('id')->eq($id)->fetch();
        $getsort = $this->getsort($process->department_id);
        $parentid = $this->getparentid($process->department_id);
       $this->dao->update(TABLE_PROCESS)
            ->set('category')->eq($category)
            ->set('categoryscore')->eq($typesscore)
           ->set('categoryname')->eq($typesname)
           ->set('istrue')->eq($istrue)
            ->set('approval_status')->eq('final')
            ->set('approval_content')->eq($this->post->approval_content)
            ->set('approval_time')->eq(helper::now(time()))
           ->set('supervision_approver')->eq($headuser->id_card)
           ->set('score')->eq($score)
           ->set('scoretype')->eq($scoretype)
           ->set('scoretypename')->eq($scoretypename)
           ->set('sort')->eq($getsort)
           ->set('parent_id')->eq($parentid)
            ->where('id')->eq($id)
            ->exec();
        $order = $this->dao->select('orders,eventtime')->from(TABLE_PROCESS)->where('id')->eq($id)->fetch();
        for($i=0;$i<=count($minicategory);$i++){
            $mininame   = $this->dao->select("id,mistake,pid,$scoretype")->from(TABLE_CATEGORY)->where('id')->eq($minicategory[$i])->fetch();
            if($mininame){
                $bigname = $this->dao->select('*')->from(TABLE_BIGCATEGORY)->where('id')->eq($mininame->pid)->fetch();
                $this->dao->insert(TABLE_DEDUCTMARKS)
                    ->set('bigid')->eq($bigname->id)
                    ->set('bigname')->eq($bigname->mistake)
                    ->set('categoryid')->eq($mininame->id)
                    ->set('categoryname')->eq($mininame->mistake)
                    ->set('score')->eq($mininame->$scoretype)
                    ->set('processid')->eq($order->orders)
                    ->set('createting')->eq($order->eventtime)
                    ->set('state')->eq('final')
                    ->set('istrue')->eq($istrue)
                    ->set('scoretype')->eq($scoretype)
                    ->set('scoretypename')->eq($scoretypename)
                    ->exec();

                $this->dao->insert(TABLE_STATISTICS)
                    ->set('monitors_idcard')->eq($process->monitors_idcard)
                    ->set('monitors_department_id')->eq($process->monitors_department_id)
                    ->set('monitors_department_name')->eq($process->monitors_department_name)
                    ->set('m_inputtime')->eq($process->m_inputtime)
                    ->set('eventtime')->eq($process->eventtime)
                    ->set('address')->eq($process->address)
                    ->set('department_id')->eq($process->department_id)
                    ->set('department_name')->eq($process->department_name)
                    ->set('sort')->eq($getsort)
                    ->set('department_heads')->eq($process->department_heads)
                    ->set('personnel')->eq($process->personnel)
                    ->set('persontime')->eq($process->persontime)
                    ->set('bigcategory')->eq($bigname->id)
                    ->set('bigcategoryname')->eq($bigname->mistake)
                    ->set('category')->eq($mininame->id)
                    ->set('categoryname')->eq($mininame->mistake)
                    ->set('categoryscore')->eq($mininame->$scoretype)
                    ->set('confirmedtime')->eq($process->confirmedtime)
                    ->set('supervision_approver')->eq($process->supervision_approver)
                    ->set('approval_time')->eq($process->approval_time)
                    ->set('approval_status')->eq($process->approval_status)
                    ->set('istrue')->eq($process->istrue)
                    ->set('orders')->eq($process->orders)
                    ->set('revocation')->eq($process->revocation)
                    ->set('parent_id')->eq($parentid)
                    ->set('liandai')->eq('no')
                    ->set('scoretype')->eq($scoretype)
                    ->set('scoretypename')->eq($scoretypename)
                    ->exec();
            }

        }

            $unit = $this->post->unit1;
            $user = $this->post->user1;
            $bigcategory = $this->post->bigcategory1;
            $category = $this->post->category1;

        if($user&&$category){
            $unitname = $this->getunitname($unit);

            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);

            $score = $this->getscore($scoretype,$category);

            $orders = $this->getorders($id);

            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }

        $unit = $this->post->unit2;
        $user = $this->post->user2;
        $bigcategory = $this->post->bigcategory2;
        $category = $this->post->category2;
        if($user&&$category){
            $unitname = $this->getunitname($unit);
            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);
            $score = $this->getscore($scoretype,$category);
            $orders = $this->getorders($id);
            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();

            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }

        $unit = $this->post->unit3;
        $user = $this->post->user3;
        $bigcategory = $this->post->bigcategory3;
        $category = $this->post->category3;
        if($user&&$category){
            $unitname = $this->getunitname($unit);
            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);
            $score = $this->getscore($scoretype,$category);
            $orders = $this->getorders($id);

            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();

            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }


        $unit = $this->post->unit4;
        $user = $this->post->user4;
        $bigcategory = $this->post->bigcategory4;
        $category = $this->post->category4;
        if($user&&$category){
            $unitname = $this->getunitname($unit);
            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);
            $score = $this->getscore($scoretype,$category);
            $orders = $this->getorders($id);
            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();

            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }

        $unit = $this->post->unit5;
        $user = $this->post->user5;
        $bigcategory = $this->post->bigcategory5;
        $category = $this->post->category5;
        if($user&&$category){
            $unitname = $this->getunitname($unit);
            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);
            $score = $this->getscore($scoretype,$category);
            $orders = $this->getorders($id);
            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();

            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }

        $unit = $this->post->unit6;
        $user = $this->post->user6;
        $bigcategory = $this->post->bigcategory6;
        $category = $this->post->category6;
        if($user&&$category){
            $unitname = $this->getunitname($unit);
            $bname = $this->getbigcategoryname($bigcategory);
            $category = implode($category,',');
            $categoryname = $this->getcategoryname($category);
            $score = $this->getscore($scoretype,$category);
            $orders = $this->getorders($id);
            $this->dao->insert(TABLE_LIANDAI)
                ->set('orders')->eq($orders)
                ->set('unit_id')->eq($unit)
                ->set('unit_name')->eq($unitname)
                ->set('user')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('score')->eq($score)
                ->set('eventtime')->eq($order->eventtime)
                ->set('istrue')->eq($istrue)
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();

            $uscor = $this->getsort($unit);
            $parentid = $this->getparentid($unit);
            $this->dao->insert(TABLE_STATISTICS)
                ->set('monitors_idcard')->eq($process->monitors_idcard)
                ->set('monitors_department_id')->eq($process->monitors_department_id)
                ->set('monitors_department_name')->eq($process->monitors_department_name)
                ->set('m_inputtime')->eq($process->m_inputtime)
                ->set('eventtime')->eq($process->eventtime)
                ->set('address')->eq($process->address)
                ->set('department_id')->eq($unit)
                ->set('department_name')->eq($unitname)
                ->set('sort')->eq($uscor)
                ->set('personnel')->eq($user)
                ->set('bigcategory')->eq($bigcategory)
                ->set('bigcategoryname')->eq($bname)
                ->set('category')->eq($category)
                ->set('categoryname')->eq($categoryname)
                ->set('categoryscore')->eq($score)
                ->set('supervision_approver')->eq($process->supervision_approver)
                ->set('approval_time')->eq($process->approval_time)
                ->set('approval_status')->eq($process->approval_status)
                ->set('istrue')->eq($process->istrue)
                ->set('orders')->eq($process->orders)
                ->set('revocation')->eq($process->revocation)
                ->set('parent_id')->eq($parentid)
                ->set('liandai')->eq('yes')
                ->set('scoretype')->eq($scoretype)
                ->set('scoretypename')->eq($scoretypename)
                ->exec();
        }
        return !dao::isError();


    }

    public function getconcerned($pager){
        $user = $_SESSION['user'];
        $process = $this->dao->select('*')->from(TABLE_PROCESS)
            ->where('personnel')->like('%%'.$user->id_card.'%%')
            ->andWhere('heads_confirm')->eq('yes')
            ->andWhere('revocation')->ne('yes')
            ->orderBy('id desc')->page($pager)->fetchAll();
        foreach($process as $value){
            $bigcategory = $this->dao->select('mistake')->from(TABLE_BIGCATEGORY)->where('id')->eq($value->bigcategory)->fetch();
            $value->bigcategory = $bigcategory->mistake;
            $type =  $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($value->category)->fetchPairs();
            $value->category = implode($type,',');
            $personnel = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->in($value->personnel)->fetchPairs();
            $value->personnel = implode($personnel,',');
            $monitors = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->monitors_idcard)->fetch();
            $value->monitors_idcard = $monitors->name;
            $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($value->department_heads)->fetch();
            $value->department_heads = $head->name;
        }
        return $process;
    }

    public function getconbyid($id){
        $process = $this->getprocessbyid($id);
        $person =  $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->in($process->personnel)->fetchPairs();
        $process->idcard =explode(',',$process->personnel);
        $process->personnel = implode($person,',');
        $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($process->department_heads)->fetch();
        $process->department_heads = $head->name;
        $headadiut = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($process->supervision_approver)->fetch();
        $process->supervision_approver = $headadiut->name;
        $categoryname = $process->categoryname;
        $categoryname = explode(',',$categoryname);
        $process->categoryname = $categoryname;
        return $process;
    }

    public function getldbyorder($order){

        $liandai = $this->dao->select('*')->from(TABLE_LIANDAI)->where('orders')->eq($order)->fetchAll();
        foreach($liandai as $val){
            $val->categoryname = explode(',',$val->categoryname);
            $head = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($val->user)->fetch();
            $val->user = $head->name;
        }
        return $liandai;
    }

    public function updatescore(){

        $process = $this->dao->select('orders')->from(TABLE_PROCESS)->where('approval_status')->eq('final')->fetchAll();

        foreach($process as $val){

            $score = $this->dao->select("Sum(score)")->alias('sum')->from(TABLE_DEDUCTMARKS)->where('processid')->eq($val->orders)->fetch();
            $sum = $score->sum;
            $this->dao->update(TABLE_PROCESS)->set('score')->eq($sum)->where('orders')->eq($val->orders)->exec();
        }

        return !dao::isError();
    }


}